package com.arpa.oms.controller;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.arpa.core.utils.collection.CollectionUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.cache.PartyCache;
import com.arpa.ntocc.common.common.cache.ShipmentCache;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.exception.ServiceException;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.oms.domain.dto.BatchAddGoodsDTO;
import com.arpa.oms.domain.dto.OmsAuditSaleDTO;
import com.arpa.oms.domain.dto.SaleDto;
import com.arpa.oms.domain.dto.SaleItemDto;
import com.arpa.oms.domain.entity.Sale;
import com.arpa.oms.domain.entity.SaleItem;
import com.arpa.oms.domain.enums.OrderStatusEnum;
import com.arpa.oms.domain.vo.OmsSaleReturnVO;
import com.arpa.oms.domain.vo.SaleItemVO;
import com.arpa.oms.domain.vo.SaleVO;
import com.arpa.oms.service.IOrderDealService;
import com.arpa.oms.service.ISaleItemService;
import com.arpa.oms.service.ISaleService;
import com.arpa.wms.domain.entity.Receive;
import com.arpa.wms.domain.vo.ReceiveVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;


/**
 * <p>
 * OMS销售明细 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-10-23
 */
@Api(tags = "OMS销售明细")
@RestController
@RequestMapping(value="/oms/saleItem", produces = "application/json;charset=UTF-8")
@Log4j2(topic = "business")
public class SaleItemController {

    @Autowired
    private ISaleItemService saleItemService;
    @Autowired
    @Lazy
    private ISaleService saleService;
    @Autowired
    private ShipmentCache shipmentCache;

    /**
     * 获取商品关系对应列表
     * @param dto
     * @param request
     * @return
     */
    @ApiOperation(value = "获取商品关系对应列表", notes = "获取商品关系对应列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @GetMapping(value = "/goodsRelationList")
    public Result goodsRelationList(SaleItemDto dto, HttpServletRequest request) {
        try {
            String shipmentCode = UserUtil.getLoginWarehouseCodeOrShipmentCode();
            List<String> saleCodes = saleService.list(new LambdaQueryWrapper<Sale>().eq(Sale::getShipmentCode, shipmentCode)
                    .eq(Sale::getShopCode, dto.getShopCode()).eq(Sale::getPlatformCode, dto.getPlatformCode()))
                    .stream().map(info -> info.getCode()).collect(Collectors.toList());
            dto.setSaleCodes(saleCodes);
            SaleItemVO saleItemVo = saleItemService.goodsRelationListSum(dto);
            if (saleItemVo == null) {
                saleItemVo = new SaleItemVO();
                saleItemVo.setTotal(0L);
            }
            List<SaleItemVO> SaleItemVOS = saleItemService.goodsRelationList(dto);
            Page returnPage = new Page(dto.getPageNum(),dto.getPageSize());
            returnPage.setRecords(SaleItemVOS);
            returnPage.setTotal(saleItemVo.getTotal());
            returnPage.setSumObject(saleItemVo);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 保存商品关系对应
     * @param dtoList
     * @param request
     * @return
     */
    @ApiOperation(value = "保存商品关系对应", notes = "保存商品关系对应")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping(value = "/saveGoodsRelation")
    public Result saveGoodsRelation(@RequestBody List<SaleItemDto> dtoList, HttpServletRequest request) {
        try {
            return saleItemService.saveGoodsRelation(dtoList);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


}
